// let s = "abcabcbb";
// var lengthOfLongestSubstring = function (s) {
//     if (s.length === 0) {
//         return 0;
//     }
//     let left = 0;
//     let obj = {};
//     let maxlength = 0;
//     for (let i = 0; i < s.length; i++) {
//         if (obj[s[i]] !== undefined) {
//             // 当遇到重复字符时，更新左指针的位置
//             left = Math.max(left, obj[s[i]] + 1);
//         }
//         // 更新字符的最新位置
//         obj[s[i]] = i;
//         // 计算当前无重复子串的长度
//         maxlength = Math.max(maxlength, i - left + 1);
//     }
//     return maxlength;
// };
// console.log(lengthOfLongestSubstring(s));


let s = "pwwkew";
var lengthOfLongestSubstring = function (s) {
    if (s.length === 0) {
        return 0
    }
    let left = 0
    let maxLength = 0
    const obj = {}
    for (let i = 0; i < s.length; i++) {
        if (obj[s[i]] === undefined) {
            obj[s[i]] = i
        } else {
            maxLength = Math.max(maxLength, i - left)
            while (left < obj[s[i]] + 1) {
                left++
            }
            obj[s[i]] = i
        }
    }
    return maxLength === 0 ? 0 : maxLength
};
console.log(lengthOfLongestSubstring(s));